package Work2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class SimpleNotePad {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String filePath = "pw.txt";
        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new FileWriter(filePath, true));

            System.out.println("pw.txt：输入内容，按回车键确认，输入'exit'退出。");

            while (true) {
                String input = scanner.nextLine();
                if ("exit".equalsIgnoreCase(input)) {
                    System.out.println("程序退出。");
                    break;
                } else {
                    writer.write(input);
                    writer.newLine();
                    writer.flush();
                }
            }
        } catch (IOException e) {
            System.err.println("写入文件时发生错误: " + e.getMessage());
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                System.err.println("关闭 BufferedWriter 时发生错误: " + e.getMessage());
            }
            scanner.close();
        }
    }
}